﻿#include "TableWindow.h"
#include"ResizeWindowEventFilter.h"
#include<QTableView>
#include<QSpinBox>
#include<QDebug>
#include<QResizeEvent>
#include<QStandardItemModel>
#include<QHeaderView>
#include<QLabel>
TableWindow::TableWindow(QWidget* parent):
	QWidget(parent)
{
	m_SpinBox = new QSpinBox(this);
	m_Info = new QLabel(this);
	m_Table = new QTableView(this);
	m_Table->move(0, 40);
	m_Table_model = new QStandardItemModel(this);
	m_Table->setModel(m_Table_model);
	m_Info->setAlignment(Qt::AlignCenter);
	setTable();
	//setSpinBox();
	connect(m_SpinBox, &QSpinBox::valueChanged,this,[=]() {
		showTable(); 
		});

	installEventFilter(new ResizeWindowEventFilter([=](QResizeEvent*ev)->bool {
		int w = size().width();
	//if(w>100)
	m_Info->resize(100, 40);
	m_Info->move(w - 100, 0);
	m_SpinBox->resize(w - 100, 40);

	m_Table->resize(size() - QSize(0, 40));
	if (setSpinBox())
	{
		showTable();
	}
	setTable();
	setColumnWidth();
	return true;
		},this));
}

QTableView& TableWindow::Table()
{
	return *m_Table;
}

QStandardItemModel& TableWindow::Table_model()
{
	return *m_Table_model;
}

QSpinBox& TableWindow::SpinBox()
{
	return *m_SpinBox;
}

void TableWindow::clearData()
{
	m_data.clear();
}

void TableWindow::clearTable()
{
	m_Table_model->clear();
}

void TableWindow::insertData(const QStringList& str)
{
	m_data.push_back(str);
}

void TableWindow::setHorizontalHeading(const QStringList& str)
{
	m_HorizontalHeading = str;
	setHorizontalHeading();
}

void TableWindow::setHorizontalHeading()
{
	m_Table_model->setHorizontalHeaderLabels(m_HorizontalHeading);
}

void TableWindow::setVerticalHeading(const QStringList& str)
{
	m_VerticalHeading = str;
	setVerticalHeading();
}

void TableWindow::setVerticalHeading()
{
	m_Table_model->setVerticalHeaderLabels(m_VerticalHeading);
}

void TableWindow::setColumnWidth(const int w)
{
	m_Table->horizontalHeader()->setDefaultSectionSize(w);
}

void TableWindow::setColumnWidth()
{
	int w = size().width()-30;
	//获取列数
	int columnNum = m_Table->model()->columnCount();
	if (columnNum > 1)
	{
		setColumnWidth(w / columnNum);
	}
		
}

void TableWindow::setHorizontalAlignment(Qt::Alignment alignment)
{
	m_Table->horizontalHeader()->setDefaultAlignment(alignment);
}

void TableWindow::setRowHeight(const int h)
{
	m_Table->verticalHeader()->setDefaultSectionSize(200);
}

void TableWindow::setVerticalAlignment(Qt::Alignment alignment)
{
	m_Table->verticalHeader()->setDefaultAlignment(alignment);
}

void TableWindow::setTextAlignmentAll(Qt::Alignment alignment)
{
	m_TextAlignment = alignment;
}

void TableWindow::showTable()
{
	m_Table_model->clear();
	setHorizontalHeading();
	setVerticalHeading();
	int startSel = (m_SpinBox->value()-1) * m_onePagenum;
	int curSel = 0;
	for (int nSel = startSel;nSel< m_data.size()&& nSel- startSel<m_onePagenum;++nSel)
	{
		auto strList = m_data[nSel];
		int column = 0;
		for (auto& str : strList)
		{
			m_Table_model->setItem(curSel, column, new QStandardItem(str));
			++column;
		}
		++curSel;
	}
	setTextAlignmentAll();
}

void TableWindow::initRun()
{
	setSpinBox();
	setTable();
	showTable();
}

const bool TableWindow::setSpinBox()
{
	int h = m_Table->size().height();
	if (h < 1)
	{
		qWarning() << "表格总高度有问题:" << h;
		return false;
	}
	int rowH = rowHeight();
	if (rowH < 1)
	{
		qWarning() << "表格行高有问题:" << rowH;
		return false;
	}
	int onePagenum= h / rowH - 1;
	if (onePagenum == 0)
		return false;
	if (m_onePagenum == onePagenum)
		return false;
	m_onePagenum = onePagenum;
	int count = m_data.size();
	m_pageNum = count / m_onePagenum;
	if (count % m_onePagenum)
		++m_pageNum;
	if (m_pageNum == 0)
	{
		m_SpinBox->setEnabled(false);
		m_onePagenum = 0;
		return false;
	}
	else
	{
		m_SpinBox->setEnabled(true);
		m_SpinBox->setRange(1, m_pageNum);
	}
	return true;
}

void TableWindow::setTable() 
{
	QString buf = QString("%1页/%2条").arg(m_pageNum).arg(m_data.count());
	m_Info->setText(buf);
}

void TableWindow::setTextAlignmentAll()
{
	//获取列数
	int columnNum = m_Table->model()->columnCount();
	//获取行数
	int RowNum = m_Table->model()->rowCount();

	for (size_t i = 0; i < RowNum; i++)
	{
		for (size_t j = 0; j < columnNum; j++)
		{
			m_Table_model->item(i, j)->setTextAlignment(m_TextAlignment);
		}
	}
}

const int TableWindow::columnWidth()
{
	return m_Table->horizontalHeader()->defaultSectionSize();
}

const int TableWindow::rowHeight()
{
	return m_Table->verticalHeader()->defaultSectionSize();
}

void TableWindow::clearAll()
{
	clearData();
	clearTable();
	setSpinBox();
	setTable();
}
